﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameWP
{
    // Mau factory, load len het can thi clone ra xai
    public class DBCharacter : Database
    {
    #region Properties
        public Luffy luffy;
        public List<AutoCharacter> Zombies;
        public List<AutoCharacter> AHeros;
        public List<Texture2D> ListIconAHeros;
        public List<Trap> ListTraps;
        public List<Texture2D> ListIconInShop;
        public Treasure treasure;
        private float InitPositionZombieX;
        private float InitPositionZombieY;
        private float InitPositionAHeroX;
        private float InitPositionAHeroY;
    #endregion

    #region Initiation
        public DBCharacter(ContentManager content, GraphicsDeviceManager graphic)
        {
            _content = content;
            _graphic = graphic;

            // New
            Zombies = new List<AutoCharacter>();
            InitPositionZombieX = 1900; InitPositionZombieY = 270;
            AHeros = new List<AutoCharacter>();
            InitPositionAHeroX = 20; InitPositionAHeroY = 270;

            //Load
            LoadListZombie();
            LoadListAHero();
            LoadLuffy();
            LoadTreasure();
            LoadListTrap();
        }
    #endregion        

    #region Methods for load
        private List<Texture2D> LoadListTexture(int CountTexture, string path)
        {
            List<Texture2D> listTexture = new List<Texture2D>();        // danh sach all cac texture
            for (int i = 0; i < CountTexture; i++)
            {
                Texture2D temp = _content.Load<Texture2D>(path + i.ToString("00"));
                listTexture.Add(temp);
            }
            return listTexture;
        }

        private List<int> LoadSeq(int start, int end)
        {
            List<int> seqtemp = new List<int>();
            for (int i = start; i < end; i++)
                seqtemp.Add(i);
            return seqtemp;
        }

        private List<List<int>> LoadListSequence(int move, int fight, int dead)
        {
            List<List<int>> seqTexture = new List<List<int>>();
            seqTexture.Add(LoadSeq(0, move));   //Add seq Move
            seqTexture.Add(LoadSeq(move, fight));   //Add seq Fight
            seqTexture.Add(LoadSeq(fight, dead));   //Add seq Dead
            return seqTexture;
        }

        private List<List<int>> LoadListSequenceWithBullet(int move, int fight, int dead, int bullet)
        {
            List<List<int>> seqTexture = new List<List<int>>();
            seqTexture.Add(LoadSeq(0, move));   //Add seq Move
            seqTexture.Add(LoadSeq(move, fight));   //Add seq Fight
            seqTexture.Add(LoadSeq(fight, dead));   //Add seq Dead
            seqTexture.Add(LoadSeq(dead, bullet));   //Add seq Bullet
            return seqTexture;
        }        

    #endregion

    #region Load Luffy
        private void LoadLuffy()
        {
            List<Texture2D> listTexture;        // danh sach all cac texture
            List<Texture2D> listIconSkill;        // danh sach all cac texture
            List<List<int>> seqTexture;         // danh sach cac hanh dong
            List<int> seqUnit;

            #region Load texture Luffy
            //Load all texture
            listTexture = new List<Texture2D>();
            for (int i = 0; i < 151; i++)
            {
                Texture2D temp = _content.Load<Texture2D>("Textures/Luffy/luffy_" + (i+1).ToString("000"));
                listTexture.Add(temp);
            }
            seqTexture = new List<List<int>>();
                        

            //Set seq action
            //Seq Stand
                seqUnit = new List<int>();
                for (int i = 0; i < 6; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            //Seq Move
                seqUnit = new List<int>();
                for (int i = 6; i < 14; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            //Seq Fight
                seqUnit = new List<int>();
                for (int i = 14; i < 28; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            //Seq Dead
                seqUnit = new List<int>();
                for (int i = 28; i < 32; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            //Seq Skill 0
                seqUnit = new List<int>();
                for (int i = 32; i < 38; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            //Seq Skill 1
                seqUnit = new List<int>();
                for (int i = 39; i <= 52; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            //Seq Skill 2
                seqUnit = new List<int>();
                for (int i = 53; i <= 71; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            //Seq Skill 3
                seqUnit = new List<int>();
                for (int i = 72; i <= 96; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            //Seq Skill 4
                seqUnit = new List<int>();
                for (int i = 97; i <= 120; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            //Seq Skill 5
                seqUnit = new List<int>();
                for (int i = 121; i <= 149; i++)
                    seqUnit.Add(i);
                //Add seq vao listSeq
                seqTexture.Add(seqUnit);
            #endregion

            #region Load content
                SpriteFont font = _content.Load<SpriteFont>("Fonts/ScoreFont");
            #endregion

            #region Load IconSkill
                listIconSkill = new List<Texture2D>();
                for (int i = 0; i < 6; i++)
                {
                    Texture2D temp = _content.Load<Texture2D>("Textures/Luffy/Icon Skill/is_" + i.ToString("00"));
                    listIconSkill.Add(temp);
                }
            #endregion
            
            //New Luffy tuy chinh thong so
                //luffy = new Luffy(listTexture, listIconSkill, new Vector2(50, 250), 2000, 10, 100, 500, 1, 1, 1, new List<int>(), font, seqTexture);
            //New luffy tu thong so doc tu save
                luffy = new Luffy(listTexture, listIconSkill, new Vector2(50, 280),
                    ManagerGame.managerSaveLoad._dataSave._hp,
                    ManagerGame.managerSaveLoad._dataSave._dam,
                    ManagerGame.managerSaveLoad._dataSave._mana,
                    ManagerGame.managerSaveLoad._dataSave._balance,
                    ManagerGame.managerSaveLoad._dataSave._level,
                    ManagerGame.managerSaveLoad._dataSave._levelSkill,
                    ManagerGame.managerSaveLoad._dataSave._levelScreen,
                    ManagerGame.managerSaveLoad._dataSave._AHeroHave,
                    font, seqTexture);
        }
    #endregion
    
    #region Load Zombies 
        private void LoadListZombie(){
            // Can thiet cho 1 zombie
            List<Texture2D> listTexture;        // danh sach all cac texture
            List<List<int>> seqTexture;         // danh sach cac hanh dong

            SwordZombie SZombie;
            BowZombie BZombie;

        #region 00 Navy Sword
            //Load all texture
            listTexture = LoadListTexture(12, "Textures/Zombie/00 Navy sword/");
            seqTexture = LoadListSequence(5, 9, 12);

            //New zombie
            SZombie = new SwordZombie(listTexture, 
                new Vector2 (InitPositionZombieX , InitPositionZombieY - 30), 
                700, 4, 2, 10, 2, 5, SpriteEffects.None, seqTexture);
            Zombies.Add(SZombie);
        #endregion

        #region 01 Navy Gun
            //Load all texture
            listTexture = LoadListTexture(12, "Textures/Zombie/01 Navy gun/");
            seqTexture = LoadListSequenceWithBullet(6, 8, 11, 12);

            //New zombie
            BZombie = new BowZombie(listTexture, 
                new Vector2(InitPositionZombieX, InitPositionZombieY - 18), 
                new Vector2(InitPositionZombieX,InitPositionZombieY - 20 - 18), 
                500, 4, 2, 400, 10, 2, 5, SpriteEffects.None, seqTexture);
            Zombies.Add(BZombie);
            #endregion

        #region 02 Ace
            //Load all texture
            listTexture = LoadListTexture(19, "Textures/Zombie/02 Ace/");
            seqTexture = LoadListSequenceWithBullet(8, 14, 18, 19);
            
            //New zombie
            BZombie = new BowZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 26 - 10),
                new Vector2(InitPositionZombieX, InitPositionZombieY + 20 - 10),
                800, 6, 5, 400, 10, 2, 10, SpriteEffects.FlipHorizontally, seqTexture);
            Zombies.Add(BZombie);
            #endregion

        #region 03 Buggy
            //Load all texture
            listTexture = LoadListTexture(18, "Textures/Zombie/03 Buggy/");
            seqTexture = LoadListSequence(7, 15, 18);
            
            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 24 - 30),
                800, 8, 4, 10, 2, 10, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -70 };
            Zombies.Add(SZombie);
            #endregion

        #region 04 Aokiji
            //Load all texture
            listTexture = LoadListTexture(24, "Textures/Zombie/04 Aokiji/");
            seqTexture = LoadListSequence(9, 21, 24);

            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 22 - 70),
                1400, 8, 2, 10, 2, 20, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -80 };
            Zombies.Add(SZombie);
            #endregion

        #region 05 Mihawk
            //Load all texture
            listTexture = LoadListTexture(18, "Textures/Zombie/05 Mihawk/");
            seqTexture = LoadListSequence(8, 15, 18);

            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 20 - 50),
                1000, 13, 2, 10, 2, 25, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -60 };
            Zombies.Add(SZombie);
            #endregion

        #region 06 Crocodile
            //Load all texture
            listTexture = LoadListTexture(29, "Textures/Zombie/06 Crocodile/");
            seqTexture = LoadListSequence(8, 25, 29);

            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 18 - 50),
                1500, 8, 3, 10, 2, 30, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -40 };
            Zombies.Add(SZombie);
            #endregion

        #region 07 Kizaru
            //Load all texture
            listTexture = LoadListTexture(25, "Textures/Zombie/07 Kizaru/");
            seqTexture = LoadListSequence(9, 22, 25);

            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 18 - 50),
                500, 8, 10, 10, 2, 35, SpriteEffects.FlipHorizontally, seqTexture) { OnlyAttackTresure = true };
            Zombies.Add(SZombie);
            #endregion

        #region 08 Jinbei
            //Load all texture
            listTexture = LoadListTexture(22, "Textures/Zombie/08 Jinbei/");
            seqTexture = LoadListSequenceWithBullet(8, 17, 21, 22);

            //New zombie
            BZombie = new BowZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 16 - 20 - 30),
                new Vector2(InitPositionZombieX, InitPositionZombieY + 60 - 26 ),
                1800, 15, 2, 300, 10, 2, 30, SpriteEffects.FlipHorizontally, seqTexture);
            Zombies.Add(BZombie);
            #endregion

        #region 09 Charloss
            //Load all texture
            listTexture = LoadListTexture(19, "Textures/Zombie/09 Charloss/");
            seqTexture = LoadListSequenceWithBullet(8, 15, 18, 19);

            //New zombie
            DoctorZombie DZombie = new DoctorZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 16 ),
                new Vector2(InitPositionZombieX, InitPositionZombieY + 10 ),
                2000, -15, 3, 300, 10, 2, 30, SpriteEffects.FlipHorizontally, seqTexture);
            Zombies.Add(DZombie);
            #endregion

        #region 10 Kuma
            //Load all texture
            listTexture = LoadListTexture(19, "Textures/Zombie/10 Kuma/");
            seqTexture = LoadListSequence(8, 15, 19);

            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 14 - 80),
                2000, 15, 3, 10, 2, 30, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -50 };
            Zombies.Add(SZombie);
            #endregion

        #region 11 Blackbeard
            //Load all texture
            listTexture = LoadListTexture(36, "Textures/Zombie/11 Blackbeard/");
            seqTexture = LoadListSequence(8, 31, 36);

            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 12 - 20 - 10),
                2000, 20, 2, 10, 2, 80, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -50 };
            Zombies.Add(SZombie);
            #endregion

        #region 12 Ivankov
            //Load all texture
            listTexture = LoadListTexture(29, "Textures/Zombie/12 Ivankov/");
            seqTexture = LoadListSequence(8, 25, 29);
                        
            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 10 - 35 - 30),
                2500, 35, 2, 10, 2, 80, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -50 } ;
            Zombies.Add(SZombie);
            #endregion

        #region 13 Garp
            //Load all texture
            listTexture = LoadListTexture(16, "Textures/Zombie/13 Garp/");
            seqTexture = LoadListSequenceWithBullet(1, 14, 15, 16); 
            
            //New zombie
            BZombie = new BowZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 8 - 40 - 50),
                new Vector2(InitPositionZombieX, InitPositionZombieY + 30 - 8 - 40),
                1500, 30, 4, 700, 10, 2, 100, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -60 };
            Zombies.Add(BZombie);
            #endregion

        #region 14 BoaHancock
            //Load all texture
            listTexture = LoadListTexture(39, "Textures/Zombie/14 BoaHancock/");
            seqTexture = LoadListSequence(8, 36, 39);

            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 6 - 15 - 30),
                3000, 40, 4, 10, 2, 120, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -30 };
            Zombies.Add(SZombie);
            #endregion

        #region 15 Magellan
            //Load all texture
            listTexture = LoadListTexture(21, "Textures/Zombie/15 Magellan/");
            seqTexture = LoadListSequenceWithBullet(8, 17, 20, 21);
            
            //New zombie
            BZombie = new BowZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 4 - 50 - 25),
                new Vector2(InitPositionZombieX, InitPositionZombieY + 40 - 4 - 40),
                4000, 40, 4, 500, 10, 2, 140, SpriteEffects.FlipHorizontally, seqTexture);
            Zombies.Add(BZombie);
            #endregion

        #region 16 Akainu
            //Load all texture
            listTexture = LoadListTexture(40, "Textures/Zombie/16 Akainu/");
            seqTexture = LoadListSequence(8, 36, 40);
            
            //New zombie
            SZombie = new SwordZombie(listTexture,
                new Vector2(InitPositionZombieX, InitPositionZombieY - 2 - 30 - 20),
                5000, 80, 5, 10, 2, 150, SpriteEffects.FlipHorizontally, seqTexture) { ScaleRect = -30 };
            Zombies.Add(SZombie);
            #endregion

        }
    #endregion

    #region Load Ahero
        private void LoadListAHero()
        {
            // Can thiet cho 1 autocharacter
            List<Texture2D> listTexture;        // danh sach all cac texture
            List<List<int>> seqTexture;         // danh sach cac hanh dong

            SwordAHero SaHero;
            BowAHero BaHero;

            #region Load IconAHero
            ListIconAHeros = new List<Texture2D>();
            for (int i = 0; i < 7; i++)
            {
                Texture2D temp = _content.Load<Texture2D>("Textures/AHero/Icon AHero/ih_" + i.ToString("00"));
                ListIconAHeros.Add(temp);
            }
            #endregion

            #region 00 Nami
                //Load all texture
            listTexture = LoadListTexture(36, "Textures/AHero/00 Nami/");
            seqTexture = LoadListSequence(8, 32, 36);

            //New hero
            SaHero = new SwordAHero(listTexture,
                new Vector2(InitPositionAHeroX, InitPositionAHeroY - 10),
                1000, 8, 3, 10, 2, 30, SpriteEffects.None, seqTexture);
            AHeros.Add(SaHero);
            #endregion

            #region 01 Ussop
            //Load all texture
            listTexture = LoadListTexture(19, "Textures/AHero/01 Ussop/");
            seqTexture = LoadListSequenceWithBullet(8, 15, 18, 19);

            //New hero
            BaHero = new BowAHero(listTexture, 
                new Vector2(InitPositionAHeroX, InitPositionAHeroY - 58), 
                new Vector2(InitPositionAHeroX, InitPositionAHeroY - 28 + 20), 
                800, 10, 3, 600, 10, 2, 40, SpriteEffects.None, seqTexture);
            AHeros.Add(BaHero);
            #endregion

            #region 02 Robin
            //Load all texture
            listTexture = LoadListTexture(35, "Textures/AHero/02 Robin/");
            seqTexture = LoadListSequence(8, 31, 35);

            //New hero
            SaHero = new SwordAHero(listTexture,
                new Vector2(InitPositionAHeroX, InitPositionAHeroY - 26),
                1500, 12, 3, 10, 2, 50, SpriteEffects.None, seqTexture);
            AHeros.Add(SaHero);
            #endregion

            #region 03 Chopper
            //Load all texture
            listTexture = LoadListTexture(24, "Textures/AHero/03 Chopper/");
            seqTexture = LoadListSequenceWithBullet(8, 19, 23, 24);

            //New hero
            BaHero = new BowAHero(listTexture,
                new Vector2(InitPositionAHeroX, InitPositionAHeroY + 20),
                new Vector2(InitPositionAHeroX, InitPositionAHeroY - 28 + 20),
                1500, -20, 2, 400, 10, 2, 40, SpriteEffects.None, seqTexture) { IsDoctor = true };
            AHeros.Add(BaHero);
            #endregion

            #region 04 Sanji
            //Load all texture
            listTexture = LoadListTexture(33, "Textures/AHero/04 Sanji/");
            seqTexture = LoadListSequence(8, 30, 33);

            //New hero
            SaHero = new SwordAHero(listTexture,
                new Vector2(InitPositionAHeroX, InitPositionAHeroY - 24),
                1800, 15, 4, 10, 2, 70, SpriteEffects.None, seqTexture);
            AHeros.Add(SaHero);
            #endregion

            #region 05 Franky
            //Load all texture
            listTexture = LoadListTexture(24, "Textures/AHero/05 Franky/");
            seqTexture = LoadListSequenceWithBullet(6, 20, 23, 24);

            //New hero
            BaHero = new BowAHero(listTexture,
                new Vector2(InitPositionAHeroX, InitPositionAHeroY - 42),
                new Vector2(InitPositionAHeroX, InitPositionAHeroY - 32 + 10),
                1200, 15, 4, 500, 10, 2, 70, SpriteEffects.None, seqTexture);
            AHeros.Add(BaHero);
            #endregion

            #region 06 Zoro
            //Load all texture
            listTexture = LoadListTexture(41, "Textures/AHero/06 Zoro/");
            seqTexture = LoadListSequence(8, 37, 41);

            //New hero
            SaHero = new SwordAHero(listTexture,
                new Vector2(InitPositionAHeroX, InitPositionAHeroY - 24),
                2400, 20, 5, 10, 2, 90, SpriteEffects.None, seqTexture);
            AHeros.Add(SaHero);
            #endregion
        }
    #endregion

    #region Load Treasure
        private void LoadTreasure()
        {
            List<Texture2D> listTexture;        // danh sach all cac texture

            //Load all texture
            listTexture = new List<Texture2D>();
            for (int i = 0; i < 15; i++)
            {
                Texture2D temp = _content.Load<Texture2D>("Textures/Treasure/" + i.ToString("00"));
                listTexture.Add(temp);
            }

            treasure = new Treasure(listTexture, new Vector2(20, 150), 1000, 8, 1);
        }
#endregion

    #region LoadTrap
        private void LoadListTrap()
        {
            List<Texture2D> listTexture;        // danh sach all cac texture

            #region Load IconInShop
            ListIconInShop = new List<Texture2D>();
            for (int i = 0; i < 5; i++)
            {
                Texture2D temp = _content.Load<Texture2D>("Textures/Trap/IconInShop/" + i.ToString("00"));
                ListIconInShop.Add(temp);
            }
            #endregion

            ListTraps = new List<Trap>();
            #region 00 Bomb
            //Load all texture
            listTexture = LoadListTexture(16, "Textures/Trap/00 Bomb/");

            //New hero
            BombTrap bombTrap = new BombTrap(listTexture, Vector2.Zero, 30, 1, 10, 1000, 2);
            ListTraps.Add(bombTrap);
            #endregion

            #region 01 Snow
            //Load all texture
            listTexture = LoadListTexture(10, "Textures/Trap/01 Snow/");

            //New hero
            SnowTrap snowTrap = new SnowTrap(listTexture, Vector2.Zero, 1, 10, 10, 300, 1);
            ListTraps.Add(snowTrap);
            #endregion

            #region 02 Fire
            //Load all texture
            listTexture = LoadListTexture(10, "Textures/Trap/02 Fire/");

            //New hero
            FireTrap fireTrap = new FireTrap(listTexture, Vector2.Zero, 5, 15, 10, 400, 1);
            ListTraps.Add(fireTrap);
            #endregion

        }
    #endregion
    }
}
